package com.yunfeisoft.controller.business;

import com.applet.base.BaseController;
import com.applet.utils.*;
import com.yunfeisoft.business.model.*;
import com.yunfeisoft.business.service.inter.*;
import com.yunfeisoft.enumeration.YesNoEnum;
import com.yunfeisoft.model.Attachment;
import com.yunfeisoft.service.inter.AttachmentService;
import com.yunfeisoft.service.inter.DataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: PmsProductController
 * Description: 商品表Controller
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Controller
public class PmsProductController extends BaseController {

    @Autowired
    private PmsProductService pmsProductService;
    @Autowired
    private DataService dataService;
    @Autowired
    private AttachmentService attachmentService;
    @Autowired
    private PmsProductLadderService productLadderService;
    @Autowired
    private PmsProductFullReductionService pmsProductFullReductionService;
    @Autowired
    private PmsSkuStockService skuStockService;
    @Autowired
    private PmsProductAttributeValueService productAttributeValueService;

    /**
     * 添加商品表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(PmsProduct record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "productCategoryId", "商品分类为空");
        validator.required(request, "name", "商品名称为空");
        validator.required(request, "subTitle", "副标题为空");
        validator.required(request, "brandId", "商品品牌为空");
        validator.required(request, "skuStockListStr", "商品SKU为空");
        validator.required(request, "imageIds", "商品相册为空");

        if (record.getPromotionType() == PmsProduct.ProductPromotionTypeEnum.TEHUI.getValue()) {
            validator.required(request, "promotionStartTime", "促销开始时间为空");
            validator.required(request, "promotionEndTime", "促销结束时间为空");
            validator.required(request, "promotionEndTime", "促销价格为空");
            validator.number(request, "promotionEndTime", "促销价格不合法");
        }

        if (record.getPromotionType() == PmsProduct.ProductPromotionTypeEnum.JIETI.getValue()) {
            validator.required(request, "ladderListStr", "优惠方式[阶梯价格]信息为空");
        }

        if (record.getPromotionType() == PmsProduct.ProductPromotionTypeEnum.MANJIAN.getValue()) {
            validator.required(request, "fullReductionListStr", "优惠方式[满减价格]信息为空");
        }

        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        String ladderListStr = ServletRequestUtils.getStringParameter(request, "ladderListStr", null);
        String fullReductionListStr = ServletRequestUtils.getStringParameter(request, "fullReductionListStr", null);
        String skuStockListStr = ServletRequestUtils.getStringParameter(request, "skuStockListStr", null);
        String content = ServletRequestUtils.getStringParameter(request, "content", null);
        String imageIdsStr = ServletRequestUtils.getStringParameter(request, "imageIds", null);
        String attributeParamsListStr = ServletRequestUtils.getStringParameter(request, "attributeParamsListStr", null);

        record.setContent(content);
        record.setId(KeyUtils.getKey());
        if (StringUtils.isNotBlank(ladderListStr)) {
            List<PmsProductLadder> ladderList = FastJsonUtils.jsonToList(ladderListStr, PmsProductLadder.class);
            for (PmsProductLadder ladder : ladderList) {
                if (ladder.getCount() == null || ladder.getDiscount() == null) {
                    return ResponseUtils.warn("优惠方式[阶梯价格]信息不全");
                }
                ladder.setProductId(record.getId());
            }
            record.setLadderList(ladderList);
        }

        if (StringUtils.isNotBlank(fullReductionListStr)) {
            List<PmsProductFullReduction> fullReductionList = FastJsonUtils.jsonToList(fullReductionListStr, PmsProductFullReduction.class);
            for (PmsProductFullReduction fullReduction : fullReductionList) {
                if (fullReduction.getFullPrice() == null || fullReduction.getReducePrice() == null) {
                    return ResponseUtils.warn("优惠方式[满减价格]信息不全");
                }
                fullReduction.setProductId(record.getId());
            }
            record.setFullReductionList(fullReductionList);
        }

        if (StringUtils.isNotBlank(skuStockListStr)) {
            List<PmsSkuStock> skuStockList = FastJsonUtils.jsonToList(skuStockListStr, PmsSkuStock.class);
            for (PmsSkuStock skuStock : skuStockList) {
                if (skuStock.getPrice() == null) {
                    return ResponseUtils.warn("商品SKU信息销售价格不全");
                }
                skuStock.setProductId(record.getId());
            }
            record.setSkuStockList(skuStockList);
        }

        if (StringUtils.isNotBlank(skuStockListStr)) {
            List<PmsProductAttributeValue> productAttributeValueList = FastJsonUtils.jsonToList(attributeParamsListStr, PmsProductAttributeValue.class);
            for (PmsProductAttributeValue productAttributeValue : productAttributeValueList) {
                productAttributeValue.setProductId(record.getId());
            }
            record.setProductAttributeValueList(productAttributeValueList);
        }

        if (StringUtils.isNotBlank(imageIdsStr)) {
            record.setImageIds(imageIdsStr.split(","));
        }

        record.setSale(0);
        record.setVerifyStatus(PmsProduct.ProductVerifyStatusEnum.NEW.getValue());
        pmsProductService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改商品表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(PmsProduct record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "productCategoryId", "商品分类为空");
        validator.required(request, "name", "商品名称为空");
        validator.required(request, "subTitle", "副标题为空");
        validator.required(request, "brandId", "商品品牌为空");
        validator.required(request, "skuStockListStr", "商品SKU为空");
        validator.required(request, "imageIds", "商品相册为空");

        if (record.getPromotionType() == PmsProduct.ProductPromotionTypeEnum.TEHUI.getValue()) {
            validator.required(request, "promotionStartTime", "促销开始时间为空");
            validator.required(request, "promotionEndTime", "促销结束时间为空");
            validator.required(request, "promotionEndTime", "促销价格为空");
            validator.number(request, "promotionEndTime", "促销价格不合法");
        }

        if (record.getPromotionType() == PmsProduct.ProductPromotionTypeEnum.JIETI.getValue()) {
            validator.required(request, "ladderListStr", "优惠方式[阶梯价格]信息为空");
        }

        if (record.getPromotionType() == PmsProduct.ProductPromotionTypeEnum.MANJIAN.getValue()) {
            validator.required(request, "fullReductionListStr", "优惠方式[满减价格]信息为空");
        }

        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        String ladderListStr = ServletRequestUtils.getStringParameter(request, "ladderListStr", null);
        String fullReductionListStr = ServletRequestUtils.getStringParameter(request, "fullReductionListStr", null);
        String skuStockListStr = ServletRequestUtils.getStringParameter(request, "skuStockListStr", null);
        String content = ServletRequestUtils.getStringParameter(request, "content", null);
        String imageIdsStr = ServletRequestUtils.getStringParameter(request, "imageIds", null);
        String attributeParamsListStr = ServletRequestUtils.getStringParameter(request, "attributeParamsListStr", null);

        record.setContent(content);
        if (StringUtils.isNotBlank(ladderListStr)) {
            List<PmsProductLadder> ladderList = FastJsonUtils.jsonToList(ladderListStr, PmsProductLadder.class);
            for (PmsProductLadder ladder : ladderList) {
                if (ladder.getCount() == null || ladder.getDiscount() == null) {
                    return ResponseUtils.warn("优惠方式[ 阶梯价格]信息不全");
                }
                ladder.setId(null);
                ladder.setProductId(record.getId());
            }
            record.setLadderList(ladderList);
        }

        if (StringUtils.isNotBlank(fullReductionListStr)) {
            List<PmsProductFullReduction> fullReductionList = FastJsonUtils.jsonToList(fullReductionListStr, PmsProductFullReduction.class);
            for (PmsProductFullReduction fullReduction : fullReductionList) {
                if (fullReduction.getFullPrice() == null || fullReduction.getReducePrice() == null) {
                    return ResponseUtils.warn("优惠方式[ 满减价格]信息不全");
                }
                fullReduction.setId(null);
                fullReduction.setProductId(record.getId());
            }
            record.setFullReductionList(fullReductionList);
        }

        if (StringUtils.isNotBlank(skuStockListStr)) {
            List<PmsSkuStock> skuStockList = FastJsonUtils.jsonToList(skuStockListStr, PmsSkuStock.class);
            for (PmsSkuStock skuStock : skuStockList) {
                if (skuStock.getPrice() == null) {
                    return ResponseUtils.warn("商品SKU信息销售价格不全");
                }
                skuStock.setId(null);
                skuStock.setProductId(record.getId());
            }
            record.setSkuStockList(skuStockList);
        }

        if (StringUtils.isNotBlank(skuStockListStr)) {
            List<PmsProductAttributeValue> productAttributeValueList = FastJsonUtils.jsonToList(attributeParamsListStr, PmsProductAttributeValue.class);
            for (PmsProductAttributeValue productAttributeValue : productAttributeValueList) {
                productAttributeValue.setId(null);
                productAttributeValue.setProductId(record.getId());
            }
            record.setProductAttributeValueList(productAttributeValueList);
        }

        if (StringUtils.isNotBlank(imageIdsStr)) {
            record.setImageIds(imageIdsStr.split(","));
        }

        pmsProductService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询商品表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        PmsProduct record = pmsProductService.load(id);
        if (record != null) {
            String content = dataService.loadByRefId(id);
            record.setContent(content);

            if (record.getPromotionType() == PmsProduct.ProductPromotionTypeEnum.JIETI.getValue()) {
                List<PmsProductLadder> ladderList = productLadderService.queryByProductId(id);
                record.setLadderList(ladderList);
            }

            if (record.getPromotionType() == PmsProduct.ProductPromotionTypeEnum.MANJIAN.getValue()) {
                List<PmsProductFullReduction> fullReductionList = pmsProductFullReductionService.queryByProductId(id);
                record.setFullReductionList(fullReductionList);
            }

            List<Attachment> attachmentList = attachmentService.queryByRefId(id);
            record.setAttachmentList(attachmentList);

            List<PmsSkuStock> skuStockList = skuStockService.queryByProductId(id);
            record.setSkuStockList(skuStockList);

            List<PmsProductAttributeValue> attributeValues = productAttributeValueService.queryByProductId(id);
            record.setProductAttributeValueList(attributeValues);
        }
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询商品表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String name = ServletRequestUtils.getStringParameter(request, "name", null);

        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("name", name);

        Page<PmsProduct> page = pmsProductService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除商品表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }

    /**
     * 提交审核
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/submit", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response submit(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductService.modifyVerifyStatus(id, PmsProduct.ProductVerifyStatusEnum.NEW.getValue(), null);
        return ResponseUtils.success("操作成功");
    }

    /**
     * 审核通过
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/acceptVerifyStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response acceptVerifyStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductService.modifyVerifyStatus(id, PmsProduct.ProductVerifyStatusEnum.ACCEPT.getValue(), null);
        return ResponseUtils.success("操作成功");
    }

    /**
     * 审核拒绝
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/rejectVerifyStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response rejectVerifyStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        String remark = ServletRequestUtils.getStringParameter(request, "remark", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        if (StringUtils.isBlank(remark)) {
            return ResponseUtils.warn("原因为空");
        }
        pmsProductService.modifyVerifyStatus(id, PmsProduct.ProductVerifyStatusEnum.REJECT.getValue(), remark);
        return ResponseUtils.success("操作成功");
    }

    /**
     * 上架
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/acceptPublishStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response acceptPublishStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductService.modifyPublishStatus(id, YesNoEnum.YES_ACCPET.getValue());
        return ResponseUtils.success("操作成功");
    }

    /**
     * 下架
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/rejectPublishStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response rejectPublishStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductService.modifyPublishStatus(id, YesNoEnum.NO_CANCEL.getValue());
        return ResponseUtils.success("操作成功");
    }

    /**
     * 设置新品
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/acceptNewStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response acceptNewStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductService.modifyNewStatus(id, YesNoEnum.YES_ACCPET.getValue());
        return ResponseUtils.success("操作成功");
    }

    /**
     * 设置非新品
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/rejectNewStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response rejectNewStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductService.modifyNewStatus(id, YesNoEnum.NO_CANCEL.getValue());
        return ResponseUtils.success("操作成功");
    }

    /**
     * 设置推荐
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/acceptRecommandStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response acceptRecommandStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductService.modifyRecommandStatus(id, YesNoEnum.YES_ACCPET.getValue());
        return ResponseUtils.success("操作成功");
    }

    /**
     * 设置非推荐
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/rejectRecommandStatus", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response rejectRecommandStatus(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductService.modifyRecommandStatus(id, YesNoEnum.NO_CANCEL.getValue());
        return ResponseUtils.success("操作成功");
    }

    /**
     * 查询已上架商品
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/queryPublishList", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response queryPublishList(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        params.put("publishStatus", YesNoEnum.YES_ACCPET.getValue());
        List<PmsProduct> list = pmsProductService.queryList(params);
        return ResponseUtils.success(list);
    }

    /**
     * 按照条件查询商品列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProduct/listAll", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response listAll(HttpServletRequest request, HttpServletResponse response) {
        String queryString = ServletRequestUtils.getStringParameter(request, "queryString", null);
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("queryString", queryString);

        List<PmsProduct> list = pmsProductService.listAll(params);
        return ResponseUtils.success(list);
    }
}
